package com.wxc.api.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.wxc.api.admin.aop.MyLog;
import com.wxc.api.admin.entity.SysMenu;
import com.wxc.api.admin.mapper.SysMenuMapper;
import com.wxc.api.admin.service.ISysMenuService;
import com.wxc.common.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

 /**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2022-04-28
 */
@RestController
@RequestMapping("/api.admin/sysMenu")
public class SysMenuController {

    @Autowired
    private ISysMenuService menuService;

    @Resource
    private SysMenuMapper menuMapper;

    // 菜单列表
    @MyLog(value = "获取菜单")
    @RequestMapping("list")
    @SaCheckPermission(value = "sys:menu:query", orRole = "admin")
    public Result list() {
        List<SysMenu> list = menuService.queryCatList();
        return Result.success(list);
    }

    // 菜单添加
    @MyLog(value = "添加菜单")
    @RequestMapping("add")
    @SaCheckPermission(value = "sys:menu:add", orRole = "admin")
    public Result add(
            String name, String path, String permission, Integer pid, Boolean is_show,
            @Validated() SysMenu sysMenu
    ) {
        if (menuService.add(name, permission, path, pid, is_show) == 1) {
            return Result.success();
        }
        return Result.fail();
    }

    // 菜单编辑
    @MyLog(value = "编辑菜单")
    @RequestMapping("edit")
    @SaCheckPermission(value = "sys:menu:edit", orRole = "admin")
    public Result edit(
            Integer id, String name, String path, String permission, Integer pid, Boolean is_show,
             @Validated() SysMenu sysMenu
    ) {
        if (menuService.edit(id, name, permission, path, pid, is_show) == 1) {
            return Result.success();
        }
        return Result.fail();
    }

}
